using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Helpers;
	using System.Web.Security;
    using TemplateWeb.DTO;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectHopThu()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_HOPTHU
			};
			return View(CreateViewModel(data));
		}



		public ActionResult DeleteHopThu(int id, int page = 1)
		{
			var result = _repHopThu.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_HOPTHU,
				CurrentPage = page
			};
			return View("SelectHopThu", CreateViewModel(data));
		}

		public ActionResult DetailOfHopThu(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_HOPTHU,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateHopThu(FormCollection forms)
		{
			string id = forms.GetValues("HopThu_ID")[0];
			string tieude = forms.GetValues("HopThu_TieuDe")[0];
			string noidung = forms.GetValues("HopThu_NoiDung")[0];
			string hoten = forms.GetValues("HopThu_HoTen")[0];
			string emailnguoihoi = forms.GetValues("HopThu_EmailNguoiHoi")[0];
			string ngayhoi = forms.GetValues("HopThu_NgayHoi")[0];
			string daphanhoi = forms.GetValues("HopThu_DaPhanHoi")[0];
			var hopthu = _repHopThu.SelectByID(int.Parse(id));
			hopthu.TieuDe = tieude;
			hopthu.NoiDung = noidung;
			hopthu.HoTen = hoten;
			hopthu.EmailNguoiHoi = emailnguoihoi;
			hopthu.NgayHoi = DateTime.Now;
			hopthu.DaPhanHoi = bool.Parse(daphanhoi.Replace(".", "").Replace(",", ""));
			var result = _repHopThu.Update(hopthu);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_HOPTHU,
			};
			return View("SelectHopThu", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertHopThu(FormCollection forms)
		{
			string tieude = forms.GetValues("HopThu_TieuDe")[0];
			string noidung = forms.GetValues("HopThu_NoiDung")[0];
			string hoten = forms.GetValues("HopThu_HoTen")[0];
			string emailnguoihoi = forms.GetValues("HopThu_EmailNguoiHoi")[0];
			string ngayhoi = forms.GetValues("HopThu_NgayHoi")[0];
			string daphanhoi = forms.GetValues("HopThu_DaPhanHoi")[0];
			var newItem = new HopThuDTO
			{
				TieuDe = tieude,
				NoiDung = noidung,
				HoTen = hoten,
				EmailNguoiHoi = emailnguoihoi,
				NgayHoi = ngayhoi == "" ? DateTime.Now : DateTime.Parse(ngayhoi),
				DaPhanHoi = bool.Parse(daphanhoi)
			};
			var result = _repHopThu.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_HOPTHU,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectHopThu", CreateViewModel(data));
		}


	}
}